package com.second.app.thread.reentrant.conditioncorrect;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author gyz
 * @date 2021/2/1 15:41
 */
public class ReentrantLockConfig {

    private Lock lock = new ReentrantLock();

    public Condition condition = lock.newCondition();


    /**
     * 睡眠方法
     */
    public void await() {
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName() + "：拿到锁了");
            condition.await();
            System.out.println("我又活了，哈哈哈哈哈");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }


    /**
     * 唤醒方法
     */
    public void signal() {
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName() + "：唤醒时间为" + System.currentTimeMillis());
            condition.signal();
        } finally {
            lock.unlock();
        }

    }

}
